package com.itfitness.butterknifedemo

import android.app.Activity
import android.util.Log
import android.view.View
import android.widget.TextView
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField

/**
 *
 * @ProjectName:    ButterknifeDemo
 * @Package:        com.itfitness.butterknifedemo
 * @ClassName:      ButterKnife
 * @Description:     java类作用描述
 * @Author:         itfitness
 * @CreateDate:     2022/1/6 20:45
 * @UpdateUser:     更新者：
 * @UpdateDate:     2022/1/6 20:45
 * @UpdateRemark:   更新说明：
 */
object ButterKnife {
    fun bind(activity:Activity){
        val activityClazz = activity::class
        //遍历类中的属性
        activityClazz.declaredMemberProperties.forEach {viewField->
            //找出有BindView注解的属性
            if(viewField.hasAnnotation<BindView>()){
                //给属性赋值
                viewField.findAnnotation<BindView>()?.let { bindViewAnnotation->
                    viewField.isAccessible = true
                    viewField.javaField?.set(activity,activity.findViewById(bindViewAnnotation.viewId))
                }
            }
        }
    }
}